//
//  UIDevice+EJAdd.m
//  JoyssomTool
//
//  Created by LiuShuang on 2019/8/6.
//  Copyright © 2019 LiuShuang. All rights reserved.
//

#import "UIDevice+EJAdd.h"

@implementation UIDevice (EJAdd)

+ (unsigned long long)systemTotalSize {
    unsigned long long total = 0;
    NSError *error = nil;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];
    if (dictionary) {
        NSNumber * size = [dictionary objectForKey:NSFileSystemSize];
        total = [size unsignedLongLongValue];
    }
    return total;
}

+ (unsigned long long)systemFreeSize {
    unsigned long long free = 0;
    NSError *error = nil;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];
    if (dictionary) {
        NSNumber * size = [dictionary objectForKey:NSFileSystemFreeSize];
        free = [size unsignedLongLongValue];
    }
    return free;
}

+ (BOOL)isEnoughFreeSizePer:(float)perentage {
    unsigned long long free = [self systemFreeSize];
    unsigned long long total = [self systemTotalSize];
    return free * 1.0 / total >= perentage;
}

+ (BOOL)isiPhoneX {
    // 根据安全区域判断
    if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone) {
        return NO;
    }
    
    
    if ([UIApplication sharedApplication].isStatusBarHidden == NO) {
        CGRect statusRect = [UIApplication sharedApplication].statusBarFrame;
        if (statusRect.size.height == 0) {
            return NO;
        }
        if (statusRect.size.height != 20) {
            return YES;
        }
        return NO;
    } else {
        if (@available(iOS 11.0, *)) {
            if ([[UIApplication sharedApplication].delegate respondsToSelector:@selector(window)]) {
                UIEdgeInsets extractedExpr = [[UIApplication sharedApplication] delegate].window.safeAreaInsets;
                CGFloat height = extractedExpr.bottom;
                return (height > 0);
            } else {
                CGRect bounds = [UIScreen mainScreen].bounds;
                CGSize size = CGSizeMake(CGRectGetWidth(bounds), CGRectGetHeight(bounds));
                CGFloat width = MIN(size.width, size.height);

                if (width <= 320) {
                    return NO;
                }
                else if (width > 320 && width < 375) {
                    return NO;
                }
                else if (width == 375) {
                    CGFloat height = MAX(size.width, size.height);
                    if (height > 667) {
                        return YES;
                    } else {
                        return NO;
                    }
                }
                else if (width > 375 && width < 414) {
                    // 390 12, 12 pro
                    return YES;
                }
                else if (width == 414) {
                    CGFloat height = MAX(size.width, size.height);
                    if (height > 736) {
                        return YES;
                    } else {
                        return NO;
                    }
                }
                else {// > 414
                    return YES;
                }
                return NO;
            }
        } else {
            return NO;
        }
    }
    
}

+ (UIEdgeInsets)safeAreaInsets {
    UIWindow * window = [UIApplication sharedApplication].windows.firstObject;
    if (![window isKeyWindow]) {
        UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
        if (CGRectEqualToRect(keyWindow.bounds, [UIScreen mainScreen].bounds)) {
            window = keyWindow;
        }
    }
    if (@available(iOS 11.0, *)) {
        UIEdgeInsets insets = [window safeAreaInsets];
        return insets;
    }
    return UIEdgeInsetsZero;
}

@end
